
package com.wyzz.hr.action;

import java.math.BigDecimal;
import java.util.*;
import org.apache.axis.utils.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IPerfcritsBO;
import com.wyzz.hr.bo.IPerfcrittypeBO;
import com.wyzz.hr.bo.IPerfschemaBO;
import com.wyzz.hr.bo.IPerfschemacritsBO;
import com.wyzz.hr.bo.IPerfschematypeBO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Perfcrits;
import com.wyzz.hr.pojo.Perfschema;
import com.wyzz.hr.pojo.Perfschemacrits;
import com.wyzz.hr.util.InterpreterForPerformance;
import com.wyzz.hr.util.StringUtil;

public class AddPerfschemaAction extends BaseAction {

	public AddPerfschemaAction() {
		msgFormulaIllegal = "\u60A8\u8F93\u5165\u7684\u516C\u5F0F{0}\u4E0D\u5408\u6CD5\uFF1A{1}";
	}

	public String executeInit() {
		actionUrl = "add";
		getDrillDownList();
		return "success";
	}

	public String execute() {
		if (StringUtils.isEmpty(perfschema.getPsName())) {
			getDrillDownList();
			addErrorInfo("\u65B9\u6848\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
			return "error";
		}
		IPerfschemaBO schemaBO = (IPerfschemaBO) getBean("perfschemaBO");
		if (schemaBO.searchPerfschemaByPsName(perfschema.getPsName()) != null) {
			getDrillDownList();
			addErrorInfo("\u65B9\u6848\u540D\u79F0\u91CD\u590D\uFF01");
			return "error";
		}
		List perfschemacritsList = getPerfschemacrits();
		if (perfschemacritsList == null) {
			getDrillDownList();
			return "error";
		}
		Employee currEmp = getCurrentEmp();
		Date now = new Date();
		perfschema.setPsCreateBy(currEmp);
		perfschema.setPsLastChangeBy(currEmp);
		perfschema.setPsCreateTime(now);
		perfschema.setPsLastChangeTime(now);
		if (!schemaBO.addPerfschema(perfschema, perfschemacritsList)) {
			getDrillDownList();
			addErrorInfo("\u6DFB\u52A0\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\uFF01");
			return "error";
		} else {
			addActionMessage("\u6DFB\u52A0\u6210\u529F\uFF01");
			return "success";
		}
	}

	private List getPerfschemacrits() {
		List critsList = new ArrayList();
		if (dataCrits.length == 0 || StringUtils.isEmpty(dataCrits[0])) {
			addActionError("\u8003\u6838\u65B9\u6848\u81F3\u5C11\u6709\u4E00\u6761\u6307\u6807");
			return null;
		}
		for (int i = 0; i < dataCrits.length; i++) {
			if (StringUtils.isEmpty(dataCrits[i]))
				continue;
			Perfschemacrits crits = new Perfschemacrits();
			crits.setPscPcId(new Perfcrits(dataCrits[i]));
			crits.setPscDataSeq(Integer.valueOf(i + 1));
			if (dataTypes[i] == null) {
				addActionError((new StringBuilder())
						.append("\u8003\u6838\u65B9\u6848\u6307\u6807")
						.append(i + 1)
						.append(
								"\u6307\u6807\u7C7B\u578B\u4E0D\u80FD\u4E3A\u7A7A\uFF01")
						.toString());
				return null;
			}
			if (dataAllowPpes[i] == null) {
				addActionError((new StringBuilder())
						.append("\u8003\u6838\u65B9\u6848\u6307\u6807")
						.append(i + 1)
						.append(
								"\u6C47\u603B\u7C7B\u578B\u4E0D\u80FD\u4E3A\u7A7A\uFF01")
						.toString());
				return null;
			}
			if (dataTypes[i].intValue() == 0
					&& !StringUtils.isEmpty(dataFormulas[i])) {
				addActionError((new StringBuilder())
						.append("\u8003\u6838\u65B9\u6848\u6307\u6807")
						.append(i + 1)
						.append(
								"\u5B9A\u6027\u7C7B\u578B\u7684\u6307\u6807\u4E0D\u9700\u8981\u8BA1\u7B97\u516C\u5F0F\uFF01")
						.toString());
				return null;
			}
			if (dataTypes[i].intValue() == 1)
				if (!StringUtils.isEmpty(dataFormulas[i])) {
					String info = validateFormula(dataFormulas[i]);
					if (!"SUCC".equals(info)) {
						addActionError(info);
						return null;
					}
					crits.setPscFormula(dataFormulas[i].toUpperCase());
				} else {
					crits.setPscFormula(null);
				}
			if (dataPercentages[i] != null
					&& StringUtil.isFloat(dataPercentages[i],
							new Integer[] { Integer.valueOf(1) })) {
				crits.setPscPercentage(new BigDecimal(dataPercentages[i]));
			} else {
				addActionError((new StringBuilder())
						.append("\u8003\u6838\u65B9\u6848\u6307\u6807")
						.append(i + 1)
						.append(
								"\u6743\u91CD\u503C\u683C\u5F0F\u9519\u8BEF\uFF0C\u5E94\u4E3A0.00-100.00\u4E4B\u95F4\u7684\u503C\uFF01")
						.toString());
				return null;
			}
			if (dataAllowPpes[i] != null) {
				crits.setPscAllowPpe(dataAllowPpes[i]);
			} else {
				addActionError((new StringBuilder())
						.append("\u8003\u6838\u65B9\u6848\u6307\u6807")
						.append(i + 1)
						.append(
								"\u6C47\u603B\u7C7B\u578B\u4E0D\u80FD\u4E3A\u7A7A\uFF01")
						.toString());
				return null;
			}
			crits.setPscClass(dataTypes[i]);
			critsList.add(crits);
		}

		return critsList;
	}

	public String validateFormula(String formula) {
		InterpreterForPerformance interpreter = new InterpreterForPerformance();
		String info = interpreter.formulaValidate(formula);
		if (!"SUCC".equals(info))
			return StringUtil.message(msgFormulaIllegal, new Object[] {
					formula, info });
		else
			return "SUCC";
	}

	private void getDrillDownList() {
		IPerfcrittypeBO crittypeBO = (IPerfcrittypeBO) getBean("perfcrittypeBO");
		crittypeList = crittypeBO.loadAll();
		IPerfschematypeBO typeBO = (IPerfschematypeBO) getBean("perfschematypeBO");
		typeList = typeBO.loadAll();
		critsTypeList = ComonBeans.getValuesToMap(
				Perfschemacrits.PROP_PSC_CLASS, new boolean[] { false });
	}

	public List getPerfcritsByPerfcrittype(String pctId) {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("addPerfschema",
				"execute")))
			return null;
		IPerfcritsBO critsBO = (IPerfcritsBO) getBean("perfcritsBO");
		List critsList = critsBO.getPerfcritsByPerfcrittype(pctId);
		List result = new ArrayList();
		Perfcrits crit;
		for (Iterator i$ = critsList.iterator(); i$.hasNext(); result.add(crit
				.getPcName())) {
			crit = (Perfcrits) i$.next();
			result.add(crit.getPcId());
		}

		return result;
	}

	public String copyPerfschema() {
		IPerfschemaBO schemaBO = (IPerfschemaBO) getBean("perfschemaBO");
		perfschema = schemaBO.loadPerfschemaById(perfschema.getPsId());
		if (perfschema == null) {
			addErrorInfo("\u53C2\u6570\u9519\u8BEF\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01");
			return "error";
		}
		perfschema.setPsName((new StringBuilder()).append(
				perfschema.getPsName()).append("(\u590D\u5236)").toString());
		IPerfschemacritsBO perfschemacritsBO = (IPerfschemacritsBO) getBean("perfschemacritsBO");
		List perfschemacritsList = perfschemacritsBO
				.loadPerfschemacritsByPsid(perfschema.getPsId());
		getDrillDownList();
		if (perfschemacritsList.isEmpty()) {
			addErrorInfo("\u53C2\u6570\u9519\u8BEF\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01");
			return "error";
		}
		int num = perfschemacritsList.size();
		dataCrittypes = new String[num];
		dataCrits = new String[num];
		dataCritNames = new String[num];
		dataTypes = new Integer[num];
		dataAllowPpes = new Integer[num];
		dataFormulas = new String[num];
		dataPercentages = new String[num];
		for (int i = 0; i < perfschemacritsList.size(); i++) {
			Perfschemacrits schemacrit = (Perfschemacrits) perfschemacritsList
					.get(i);
			dataCrittypes[i] = schemacrit.getPscPcId().getPcPctId().getPctId();
			dataCrits[i] = schemacrit.getPscPcId().getPcId();
			dataCritNames[i] = schemacrit.getPscPcId().getPcName();
			dataTypes[i] = schemacrit.getPscClass();
			dataAllowPpes[i] = schemacrit.getPscAllowPpe();
			dataFormulas[i] = schemacrit.getPscFormula();
			dataPercentages[i] = schemacrit.getPscPercentage().toString();
		}

		actionUrl = "add";
		return "success";
	}

	public List getCritsList() {
		return critsList;
	}

	public void setCritsList(List critsList) {
		this.critsList = critsList;
	}

	public List getSchemaCritsList() {
		return schemaCritsList;
	}

	public void setSchemaCritsList(List schemaCritsList) {
		this.schemaCritsList = schemaCritsList;
	}

	public List getTypeList() {
		return typeList;
	}

	public void setTypeList(List typeList) {
		this.typeList = typeList;
	}

	public Perfschema getPerfschema() {
		return perfschema;
	}

	public void setPerfschema(Perfschema perfschema) {
		this.perfschema = perfschema;
	}

	public Integer[] getDataTypes() {
		return dataTypes;
	}

	public void setDataTypes(Integer dataTypes[]) {
		this.dataTypes = dataTypes;
	}

	public String[] getDataCrits() {
		return dataCrits;
	}

	public void setDataCrits(String dataCrits[]) {
		this.dataCrits = dataCrits;
	}

	public String[] getDataFormulas() {
		return dataFormulas;
	}

	public void setDataFormulas(String dataFormulas[]) {
		this.dataFormulas = dataFormulas;
	}

	public String[] getDataPercentages() {
		return dataPercentages;
	}

	public void setDataPercentages(String dataPercentages[]) {
		this.dataPercentages = dataPercentages;
	}

	public Map getCritsTypeList() {
		return critsTypeList;
	}

	public void setCritsTypeList(Map critsTypeList) {
		this.critsTypeList = critsTypeList;
	}

	public List getCrittypeList() {
		return crittypeList;
	}

	public void setCrittypeList(List crittypeList) {
		this.crittypeList = crittypeList;
	}

	public String[] getDataCrittypes() {
		return dataCrittypes;
	}

	public void setDataCrittypes(String dataCrittypes[]) {
		this.dataCrittypes = dataCrittypes;
	}

	public Integer[] getDataAllowPpes() {
		return dataAllowPpes;
	}

	public void setDataAllowPpes(Integer dataAllowPpes[]) {
		this.dataAllowPpes = dataAllowPpes;
	}

	public String[] getDataCritNames() {
		return dataCritNames;
	}

	public void setDataCritNames(String dataCritNames[]) {
		this.dataCritNames = dataCritNames;
	}

	public String getActionUrl() {
		return actionUrl;
	}

	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}

	private static final long serialVersionUID = 1L;
	private String msgFormulaIllegal;
	private List critsList;
	private List schemaCritsList;
	private List typeList;
	private Perfschema perfschema;
	private List crittypeList;
	private Map critsTypeList;
	private String dataCrittypes[];
	private String dataCrits[];
	private String dataCritNames[];
	private Integer dataTypes[];
	private Integer dataAllowPpes[];
	private String dataFormulas[];
	private String dataPercentages[];
	private String actionUrl;
}
